package cn.inurl.logic;

import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import cn.inurl.consts.Constants;

public class HttpClientFactory {

	private HttpClientFactory() {
	}

	private static HttpClient httpclient = null;

	public static HttpClient getHttpClient() {
		if (httpclient != null) {
			return httpclient;
		}
		httpclient = new DefaultHttpClient();
		httpclient
				.getParams()
				.setParameter(
						HTTP.USER_AGENT,
						Constants.HTTP_USER_AGENT_IE7); //$NON-NLS-1$
		return httpclient;
	}

	public static String execGetMethod(String uri) {
		HttpGet httpget = new HttpGet(uri);
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		String responseBody = null;
		try {
			responseBody = getHttpClient().execute(httpget, responseHandler);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			httpget.abort();
		}
		return responseBody;
	}

	public static JSONObject execGetMethodJSON(String uri) {
		HttpGet httpget = new HttpGet(uri);
		JSONObject response = null;
		try {
			HttpResponse res = getHttpClient().execute(httpget);
			if (res.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = res.getEntity();
				String charset = EntityUtils.getContentCharSet(entity);
				response = new JSONObject(new JSONTokener(
						new InputStreamReader(entity.getContent(), charset)));
			}
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			httpget.abort();
		}
		return response;
	}

	public static String execute(HttpPost httppost,
			ResponseHandler<String> responseHandler) {
		try {
			return getHttpClient().execute(httppost, responseHandler);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			httppost.abort();
		}
		return null;
	}

	public static String execute(HttpGet httpget,
			ResponseHandler<String> responseHandler) {
		try {
			return getHttpClient().execute(httpget, responseHandler);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			httpget.abort();
		}
		return null;
	}
	
	public static HttpResponse executeNotClose(HttpGet httpget) {
		try {
			return getHttpClient().execute(httpget);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
//			httpget.abort();
		}
		return null;
	}

	public static JSONObject jsonPost(String url, JSONObject json) {
		HttpPost httppost = new HttpPost(url);
		JSONObject response = null;
		try {
			StringEntity s = new StringEntity(json.toString());
			s.setContentEncoding("UTF-8");
			s.setContentType("application/json");
			httppost.setEntity(s);

			HttpResponse res = getHttpClient().execute(httppost);
			if (res.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = res.getEntity();
				String charset = EntityUtils.getContentCharSet(entity);
				response = new JSONObject(new JSONTokener(
						new InputStreamReader(entity.getContent(), charset)));
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			httppost.abort();
		}
		return response;
	}
	public static int jsonPostWithNoJsonRet(String url, JSONObject json) {
		HttpPost httppost = new HttpPost(url);
		try {
			StringEntity s = new StringEntity(json.toString());
			s.setContentEncoding("UTF-8");
			s.setContentType("application/json");
			httppost.setEntity(s);

			HttpResponse res = getHttpClient().execute(httppost);
			if(res != null)
				return res.getStatusLine().getStatusCode();
			return 0;
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			httppost.abort();
		}
	}

}
